package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.ILoginService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Author：Chunk
 * @Description: 用户登录控制中心
 * @Date Created in 19:54 2021/4/21
 */
@RestController
@RequestMapping("/user")
public class LoginController {
    @Reference(timeout = 3000,check = false)
    ILoginService loginService;

    @Reference(timeout = 3000,check = false)
    IKaptchaService iKaptchaService;
    @Anoymous
    @PostMapping("/login")
    public ResponseData login(@RequestBody Map<String, String> map, HttpServletRequest request, HttpServletResponse httpServletResponse){
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");

        //验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String captcha_uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setCode(captcha);
        kaptchaCodeRequest.setUuid(captcha_uuid);
        KaptchaCodeResponse response = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        String code = response.getCode();
        if(!code.equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }

        //校验登录账号和密码
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setPassword(userPwd);
        userLoginRequest.setUserName(userName);
        UserLoginResponse login = loginService.login(userLoginRequest);
        //设置cookie
        Cookie cookie = new Cookie("access_token", login.getToken());
        cookie.setPath("/");
        httpServletResponse.addCookie(cookie);
        if(login.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil<UserLoginResponse>().setData(login);
        }
        return new ResponseUtil<>().setErrorMsg(login.getMsg());
    }

    @GetMapping("/login")
    public ResponseData validLogin(HttpServletRequest request){
        //获得cookie中的token信息
        String access_token = CookieUtil.getCookieValue(request, "access_token");

        CheckAuthRequest checkAuthRequest = new CheckAuthRequest();
        checkAuthRequest.setToken(access_token);
        //验证是否登录
        CheckAuthResponse checkAuthResponse = loginService.validLogin(checkAuthRequest);
        if (!checkAuthResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(checkAuthResponse.getMsg());
        }
        return new ResponseUtil<CheckAuthResponse>().setData(checkAuthResponse);
    }

}
